package cn.wolfcode.tieba.controller;

import cn.wolfcode.tieba.domain.Posted;
import cn.wolfcode.tieba.domain.Reply;
import cn.wolfcode.tieba.service.IPostedService;
import cn.wolfcode.tieba.service.IReplyService;
import cn.wolfcode.tieba.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
* 发帖表控制层
*/
@RestController
@RequestMapping("posted")
public class PostedController {

    @Autowired
    private IPostedService postedService;
    @Autowired
    private IReplyService replyService;

    @GetMapping("/list")
    public Object list(){
        List<Posted> list = postedService.list();
        System.out.println(list);
        return list;
    }


    @GetMapping("/get")
    public Object get(Long id){
        //查出指定id的帖子
        Posted posted = postedService.getById(id);
        //获取指定帖子下的所有评论
        List<Reply> replys = replyService.listById(posted.getId());
        posted.setReplys(replys);
        return posted;
    }

    @GetMapping("/save")
    public Object save(Posted posted){
        //发帖
        posted.setCreateTime(new Date());
        postedService.save(posted);
        return JsonResult.success();
    }
}
